<template>
  <master title="文件管理">
    <div class="searchForm">
      <div class="searchFormInputs">
        <el-input
          class="searchFormInput"
          v-model="params.fileName"
          placeholder="按文件名查找"
        ></el-input>
      </div>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="searchList"
      >查询</el-button>
      <div class="searchFormBtn">
        <el-button
          type="danger"
          icon="el-icon-plus"
          @click="edit()"
        >增加</el-button>
      </div>
    </div>
    <div class="el-table">
      <el-table :data="rows" stripe style="width: 100%">
        <el-table-column prop="fileName" label="文件名">
        </el-table-column>
        <el-table-column prop="fileType" label="文件类型">
        </el-table-column>
        <el-table-column prop="fileSize" label="文件大小">
        </el-table-column>
        <el-table-column prop="uploadTime" label="上传时间">
        </el-table-column>
        <el-table-column prop="uploader" label="上传人">
        </el-table-column>
        <el-table-column label="操作" width="320">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="remove(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="el-pagination">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="params.pageNum"
        :page-sizes="[10, 100, 200, 300, 400]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog :title="editForm.id ? '编辑文件' : '新增文件'" :visible.sync="visible">
      <el-form ref="editForm" :model="editForm">
        <el-form-item label="文件名">
          <el-input
            v-model="editForm.fileName"
            placeholder="文件名"
          ></el-input>
        </el-form-item>
        <el-form-item label="文件类型">
          <el-select v-model="editForm.fileType" placeholder="请选择文件类型">
            <el-option label="普通文件" value="file"></el-option>
            <el-option label="图片" value="image"></el-option>
            <el-option label="视频" value="video"></el-option>
            <el-option label="语音" value="audio"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文件链接">
          <el-input
            v-model="editForm.fileUrl"
            placeholder="文件链接"
          ></el-input>
        </el-form-item>
        <el-form-item label="文件大小">
          <el-input
            v-model="editForm.fileSize"
            placeholder="文件大小"
          ></el-input>
        </el-form-item>
        <el-form-item label="上传时间">
          <el-date-picker
            v-model="editForm.uploadTime"
            type="date"
            placeholder="选择日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="上传人">
          <el-input
            v-model="editForm.uploader"
            placeholder="上传人"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            type="textarea"
            v-model="editForm.description"
            placeholder="描述"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: right;">
          <el-button
            type="primary"
            @click="visible = false"
          >取消</el-button>
          <el-button
            type="danger"
            @click="save"
          >保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </master>
</template>
<script type="text/javascript">
(function (G) {
G.vue({
  "usingComponents": {///引入组件
    "master": "/components/Master/Master"///母版组件
  },
  "enablePullDownRefresh": true
}, {
  data: {
    loading: false,
    params: {
      pageSize: 10,
      pageNum: 1,
      fileName: ""
    },
    total: 0,
    rows: [],
    visible: false,
    editForm: {},
    enterprise: []
  },
  methods: {
    edit(row) {
      if (row) {}
      this.editForm = row ? row : {
        fileName: '',
        fileType: '',
        fileUrl: '',
        fileSize: '',
        uploadTime: '',
        uploader: '',
        description: ''
      }
      this.visible = true
    },
    save() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          if (this.editForm.id) {
            G.put('/api/model/filemanage', this.editForm).then(res => {
              if (!res.errorMsg) {
                this.visible = false
                G.toask('编辑文件成功')
                this.params.pageNum = 1
                this.getList()
              }
            })
          } else {
            G.post('/api/model/filemanage', this.editForm).then(res => {
              if (!res.errorMsg) {
                this.visible = false
                G.toask('添加文件成功')
                this.params.pageNum = 1
                this.getList()
              }
            })
          }
        }
      })
    },
    remove(row) {
      G.del(`/api/model/filemanage/${row.id}`).then(res => {
        if (!res.errorMsg) {
          G.toask('删除文件成功')
          this.params.pageNum = 1
          this.getList()
        }
      })
    },
    sizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.pageNum = 1;
      this.getList();
    },
    currentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.getList();
    },
    searchList() {
      this.params.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      G.get('/api/model/filemanage', this.params).then((res) => {
        this.loading = false;
        this.total = res.total;
        this.rows = res.rows;
      });
    }
  },
  mounted() {
    this.getList();
  }
});
})(Y)
</script>
<style scoped>
  
</style>